package org.xinjiang.tarim.travel.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.xinjiang.tarim.travel.mapper.ImpressionMapper;
import org.xinjiang.tarim.travel.model.*;
import org.xinjiang.tarim.travel.service.*;
import org.xinjiang.tarim.travel.vo.Criteria;
import org.xinjiang.tarim.travel.vo.PageBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ImpressionServiceImpl implements ImpressionService {
    @Autowired
    ImpressionMapper impressionMapper;
    @Autowired
    FileService fileService;
    @Autowired
    ImpressionFileService impressionFileService;
    @Autowired
    TopicService topicService;
    @Autowired
    TopicImpressionService topicImpressionService;
    @Autowired
    UserService userService;
    @Autowired
    RouteService routeService;


    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    @Override
    public Long insert(Impression impression, List<File> files, List<Topic> topics) {
        impressionMapper.insert(impression);
        Long impressionId = impression.getId();
        List<ImpressionFile> impressionFiles = new ArrayList<>();
        for (File file : files) {
            ImpressionFile impressionFile = new ImpressionFile();
            impressionFile.setImpressionId(impressionId);
            impressionFile.setFileId(file.getFileId());
            impressionFiles.add(impressionFile);
        }
        if (impressionFiles.size() != 0) {
            impressionFileService.insert(impressionFiles);
        }
        List insertList = new ArrayList();
        for (Topic topic : topics) {
            TopicImpression topicImpression = new TopicImpression();
            Long topicId = topic.getId();
            if (topicId == null) {
                topic.setOpenid(impression.getOpenid());
                topic.setTopicImage(files.get(0).getFileUrl());
                Long topId = topicService.insert(topic);
                topicImpression.setTopicId(topId);
                topicImpression.setImpressionId(impressionId);
            } else {
                topicImpression.setTopicId(topicId);
                topicImpression.setImpressionId(impressionId);
            }
            insertList.add(topicImpression);
        }
        if (insertList.size() != 0) {
            topicImpressionService.insertByList(insertList);
        }
        return impression.getId();
    }

    @Override
    public Map selectByPrimaryKey(Long id) {
        Impression impression = impressionMapper.selectByPrimaryKey(id);
        User user = userService.selectByPrimaryKey(impression.getOpenid());
        RouteWithBLOBs route = routeService.selectByPrimaryKey(impression.getRouteId());

        List<ImpressionFile> impressionFiles = impressionFileService.selectByImpressionId(id);
        List<File> files = null;;
        if(impressionFiles.size() != 0){
            files = fileService.selectByPrimaryKeys(impressionFiles);
        }

        List<TopicImpression> topicImpressions = topicImpressionService.selectByImpressionId(id);
        List<Topic> topics = null;
        if(topicImpressions.size() != 0){
            topics = topicService.selectByPrimaryKeys(topicImpressions);
        }

        Map map = new HashMap<>();
        map.put("impression", impression);
        map.put("user", user);
        map.put("route", route);
        map.put("file", files);
        map.put("topic", topics);

        return map;
    }

    @Override
    public List<Map> selectByPrimaryKeys(List list) {
        List<Map> mapList = new ArrayList<>();
        if (list.size() == 0) {
            return mapList;
        }
        List<Impression> impressions = impressionMapper.selectByPrimaryKeys(list);
        for(Impression impression : impressions){
            Map map = this.selectByPrimaryKey(impression.getId());
            mapList.add(map);
        }
        return mapList;
    }

    @Override
    public PageBean<Impression> selectByOpenid(Criteria criteria, PageBean pageBean) {
        criteria.addExtField("pageIndex", (pageBean.getPageIndex() - 1) * pageBean.getPageSize());
        criteria.addExtField("pageSize", pageBean.getPageSize());
        Long totalCount = impressionMapper.selectByOpenidCount(criteria);
        List resultList = impressionMapper.selectByOpenid(criteria);
        pageBean.setTotalCount(totalCount);
        pageBean.setResultList(resultList);
        return pageBean;
    }

    @Override
    public int delete(Long id) {
        return impressionMapper.delete(id);
    }
}
